using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagement;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Content;

namespace GameStateManagementSample.QuickMath
{
    class Circle
    {
        public Vector2 Center;
        public float Radius;
        public double Angle;
        
        Texture2D m_Texture;
        Texture2D m_TexButtonOK;
        SpriteBatch m_SpriteBatch;
        SpriteFont m_Font;
        ContentManager content;

        public MyOperator[] m_Operator;

        public double AngleOld;
        bool isRotation;
        int idTouch;
        Vector2 posTouchOld;
        public Circle()
        {
            Center = Vector2.Zero;
            Radius = 0;
            Angle = 0;


        }
        public Circle(float _CenterX, float _CenterY, float _radius,ScreenManager _screenmanger)
        {
            Center = new Vector2(_CenterX, _CenterY);
            Radius = _radius;
            Angle  = 0;

            m_Operator = new MyOperator[8];
            for (int i = 0; i < 8;i++ )
            {
                m_Operator[i] = new MyOperator();
                /*m_Operator[i].m_First = MyOperator.type.Number;
                m_Operator[i].m_Sec = i;*/
            }

            m_SpriteBatch = new SpriteBatch(_screenmanger.Game.GraphicsDevice);
            

            content = new ContentManager(_screenmanger.Game.Services, "Content");
            m_Font = content.Load<SpriteFont>("gamefont");
            m_Texture = content.Load<Texture2D>(@"Image/circle");
            m_TexButtonOK = content.Load<Texture2D>(@"Image/buttonOK");
        }
        public bool Intersect(float _x, float _y)
        {
            double temp = (_x - Center.X) * (_x - Center.X) + (_y - Center.Y) * (_y - Center.Y);
            
            return (temp < (Radius*Radius));
        }
        public void InputTouch(TouchCollection _touches)
        {
            foreach (TouchLocation touch in _touches)
            {
                Vector2 vDistan = touch.Position-Center;
               /* if (vDistan.Length() < 49) continue;*/
                if (touch.State == TouchLocationState.Pressed && isRotation == false && vDistan.Length() >= 49)
                {
                    if (Intersect(touch.Position.X,touch.Position.Y))
                    {
                        AngleOld = Angle;
                        isRotation = true;
                        idTouch = touch.Id;
                        posTouchOld = touch.Position;
                    }
                }

                if (touch.State == TouchLocationState.Moved)
                {
                    if (touch.Id == idTouch && isRotation == true)
                    {

                        if (Intersect(touch.Position.X, touch.Position.Y) && vDistan.Length() > 49)
                        {

                            Vector2 Vold = posTouchOld - Center;
                            Vector2 Vnew = touch.Position - Center;
                            posTouchOld = touch.Position;
                            double temp =(Vold.X*Vnew.X + Vold.Y*Vnew.Y)/ 
                                (float)(Vold.Length()* Vnew.Length()) ;
                            if (temp > 1) temp = 1;
                            if (temp < -1) temp = -1;

                            double a =0; 
                            a= Math.Acos(temp);

                            double Aold = Math.Atan(Vold.Y / Vold.X);
                            double Anew = Math.Atan(Vnew.Y / Vnew.X);
                            if (Aold <0)
                            {
                                Aold += Math.PI * 2;
                            }
                            if (Anew <0)
                            {
                                Anew += Math.PI * 2;
                            }
                            if (Anew - Aold > 0)
                            {
                                if (Anew - Aold > Math.PI*1.75f) 
                                    Angle -= a;
                                else  Angle += a;
                            }
                            else
                            {
                                if (Anew - Aold < -Math.PI * 1.75f)
                                    Angle += a;
                                else  Angle -= a; 
                            }

                            //Angle += a;       

                            if (Angle > Math.PI*2) Angle = 0;
                            if (Angle < 0) Angle = Math.PI*2;
                            
                        }
                        else
                        {
                            isRotation = false;
                        }
                    }                    
                }
                if (touch.State == TouchLocationState.Released && touch.Id == idTouch)
                {
                    isRotation = false;
                }
            }
        }
        public void Update(GameTime gameTime)
        {
            if (!isRotation)
            {
                double temp = (int)((Angle + Math.PI * 0.125) / (float)(Math.PI * 0.25));
                temp *= Math.PI * 0.25;
                if (Angle < temp)
                {
                    Angle += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (Angle > temp) Angle = temp;
                }
                if (Angle > temp)
                {
                    Angle -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (Angle < temp) Angle = temp;
                }
            }
            


        }
        public void Draw()
        {
            Matrix matrix;
            matrix = Matrix.Identity *Matrix.CreateTranslation(-200,-200,0)* 
                Matrix.CreateRotationZ((float)Angle)*
                Matrix.CreateTranslation(600,280,0)*
                Matrix.CreateScale(1.0f,1.0f,1.0f);
          

            m_SpriteBatch.Begin(SpriteSortMode.BackToFront,
                BlendState.AlphaBlend,
                null, null, null, null, matrix);


            m_SpriteBatch.Draw(m_Texture, new Vector2(0,0), null,Color.White,0.0f,Vector2.Zero,1.0f,SpriteEffects.None,1.0f);

            Vector2 deta = new Vector2(200,200);

            for (int i = 0; i < 8;i++ )
            {
                float a = i * 45 ;
                Vector2 pos = new Vector2((float)Math.Cos(MathHelper.ToRadians(a+10)) * (Radius ) + deta.X, (float)Math.Sin(MathHelper.ToRadians(a+10)) * (Radius ) + deta.Y);
             
                m_SpriteBatch.DrawString(m_Font, m_Operator[i].ToString(),pos, Color.Blue,MathHelper.ToRadians(a+180),Vector2.Zero,new Vector2(1,1),SpriteEffects.None,0.0f);
            }
               

            m_SpriteBatch.End();

            m_SpriteBatch.Begin();
            m_SpriteBatch.Draw(m_TexButtonOK, new Vector2(Center.X - 60, Center.Y - 60), Color.White);
            m_SpriteBatch.End();
        }
        public MyOperator OperatorSelect()
        {
            int select = (int)((Angle + Math.PI + Math.PI*0.125f)/(Math.PI*0.25));
            select = select % 8;
            select = 8-select;
            if (select == 8) select = 0;

            return m_Operator[select];
        }
    }
}
